package com.abc.sort;

public class SortMain {

   public static void main(String[] args) throws Exception {
      int[] nums = new int[]{1, 2, 3, 4, 7, 8, 22, 34, 72,99, 99};

      System.out.println("数据为" + search(nums, 99));
   }

   /**
    * 二分查询
    */
   public static int search(int[] nums, int target) {
      if (nums == null) return -1;
      int left = 0, right = nums.length - 1;

      while (left <= right) {
         int mid = left + (right - left) / 2;

         System.out.println("mid=" + mid);

         if (nums[mid] == target) {
            return mid;
         }
         else if (nums[mid] < target) {
            left = mid + 1;
         }
         else if (nums[mid] > target) {
            right = mid - 1;
         }
      }
      return -1;
   }
}
